using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using ZR.Infrastructure;
using ZR.Service.BaseFrame.IService;
using ZR.Model.BaseFrame.Dto;
using ZR.Model.BaseFrame.Entity;
using ZR.Model;
using ZR.Admin.WebApi.MainModules;
using ZR.ServiceCore.Attributes;

namespace ZR.Admin.WebApi.Controllers.BaseFrame
{
    /// <summary>
    /// 部门
    /// </summary>
    public class DepartControllerBase : ApiControllerBase
    {

        /// <summary>
        /// 部门接口
        /// </summary>
        protected readonly IDepartService _DepartService;

        protected readonly MapsterAdaptEntityCfg _mapsterAdaptEntityCfg;

        public DepartControllerBase(IDepartService departService, MapsterAdaptEntityCfg mapsterAdaptEntityCfg)
        {
            _DepartService = departService;
            _mapsterAdaptEntityCfg = mapsterAdaptEntityCfg;
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <returns></returns>
        [HttpPost("add")]
        [ActionPermissionFilter(Permission = "depart:add")]
        [Log(Title = "部门", BusinessType = BusinessType.INSERT)]
        public virtual async Task AddDepart([FromBody] DepartFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<DepartEntity>(_mapsterAdaptEntityCfg).ToCreate(HttpContext, EntityExtension.AutoFillField2);
            var isOk = await _DepartService.AddDepart(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "添加失败");
            }
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <returns></returns>
        [HttpPost("modify")]
        [ActionPermissionFilter(Permission = "depart:edit")]
        [Log(Title = "部门", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ModifyDepart([FromBody] DepartFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<DepartEntity>(_mapsterAdaptEntityCfg).ToUpdate(HttpContext);
            var isOk = await _DepartService.ModifyDepart(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "depart:delete")]
        [Log(Title = "部门", BusinessType = BusinessType.DELETE)]
        public virtual async Task DeleteDepart([FromBody] DeleteDepartReq req)
        {
            var idArr = Tools.SplitAndConvert<long>(req.Ids);
            var isOk = await _DepartService.DeleteDepart(idArr, false);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "删除失败");
            }
        }

        /// <summary>
        /// 变更部门状态
        /// </summary>
        /// <returns></returns>
        [HttpPost("changeState")]
        [ActionPermissionFilter(Permission = "depart:changestate")]
        [Log(Title = "部门", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ChangeDepartState([FromBody] ChangeDepartStateReq req)
        {
            var isOk = await _DepartService.ChangeDepartState(req);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }




        /// <summary>
        /// 查询部门树选择列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("treeSelectList")]
        [ActionPermissionFilter(Permission = "depart:treeSelectList")]
        public virtual async Task<List<TreeNodeInfo>> GetDepartTreeSelectList([FromBody] GetDepartTreeSelectListReq req)
        {
            var result = await _DepartService.GetDepartTreeSelectList(req);
            return result;
        }


        /// <summary>
        /// 查询部门分页列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("pageList")]
        [ActionPermissionFilter(Permission = "depart:list")]
        public virtual async Task<PagedInfo<DepartListItemDto>> GetDepartPageList([FromBody] GetDepartPageListReq req)
        {
            var result = await _DepartService.GetDepartPageList(req);
            return result;
        }

        /// <summary>
        /// 查询部门详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        [ActionPermissionFilter(Permission = "depart:detail")]
        public virtual async Task<DepartDetailDto> GetDepartDetail(long id)
        {
            var result = await _DepartService.GetDepartDetail(id);
            return result;
        }

    }
}